package com.z.redis.datatype;

import java.util.*;

public class RedisSet implements RedisData {
    private volatile long timeout = -1;
//    private final Set<BytesWrapper> set = new HashSet<>();
    private final Set<BytesWrapper> set = Collections.synchronizedSet(new HashSet<>());

    /**
     * 添加元素
     * sadd key member [member ...]
     * @param members
     * @return 添加元素数量
     */
    public int sadd(List<BytesWrapper > members){
        return (int) members.stream().filter(set::add).count();
    }

    /**
     * 删除元素
     * srem key member [member ...]
     * @param members
     * @return 删除元素数量
     */
    public int srem(List<BytesWrapper> members){
        return (int) members.stream().filter(set::remove).count();
    }

    /**
     * 获取所有元素
     * smembers key
     * @return 所有元素
     */
    public Collection<BytesWrapper> smembers(){
        return set;
    }

    public int scard(){
        return set.size();
    }

    @Override
    public long getTimeout(){
        return timeout;
    }
    @Override
    public void setTimeout(long timeout){
        this.timeout = timeout;
    }
}
